package cuongnh.project.japanesediary.vocabulary.ui.view.action.menucontribution.category;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

import cuongnh.project.japanesediary.vocabulary.common.CommonServices;
import cuongnh.project.japanesediary.vocabulary.model.Category;
import cuongnh.project.japanesediary.vocabulary.ui.view.CategoryListView;
import cuongnh.project.japanesediary.vocabulary.ui.wizard.AddCategoryWizard;

public class EditCategoryAction implements IViewActionDelegate {

	public static final String ID = "cuongnh.project.japanesediary.vocabulary.action.EditCategory";

	protected IViewPart m_view;

	private CommonServices commonServices;

	private Category category;

	private TreeViewer treeViewer;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
	 */
	@Override
	public void init(IViewPart view) {
		m_view = view;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	@Override
	public void run(IAction action) {
		// 履歴ビュー更新
		CategoryListView view = (CategoryListView) m_view
				.getAdapter(CategoryListView.class);
		commonServices = view.getCommonServices();
		category = view.getSelectedCategory();
		treeViewer = view.getTblvCategoryList();
		AddCategoryWizard addCategoryWizard = new AddCategoryWizard(category,
				commonServices);
		WizardDialog dialog = new WizardDialog(m_view.getSite().getShell(),
				addCategoryWizard);
		if (dialog.open() == Window.OK) {
			treeViewer.refresh(category);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action
	 * .IAction, org.eclipse.jface.viewers.ISelection)
	 */
	@Override
	public void selectionChanged(IAction action, ISelection selection) {

	}

}
